<?php
/**
 * Created by PhpStorm.
 * User: 马超
 * Date: 2017/12/11
 * Time: 16:11
 */

namespace console\controllers;

use yii\console\Controller;
use yii\console\Exception;
use CFPropertyList\CFPropertyList;
use console\models\Area;
use console\models\RegionBak;

class FixController extends Controller
{


    public function actionFixPosition(){
        //require_once(__DIR__.'/../classes/CFPropertyList/CFPropertyList.php');
        //var_dump( __DIR__.'/sample.xml.plist');exit;
        $plist = new CFPropertyList( __DIR__.'/../temp/list.plist', CFPropertyList::FORMAT_XML );
        $plist = $plist->toArray();
        foreach($plist AS $key=>$value){
            /*if($key!='810000香港特别行政区' && $key!='820000澳门特别行政区')
                continue;*/
            if(!in_array($key,['110000北京市','120000天津市','310000上海市','440300深圳市','500000重庆市','810000香港特别行政区','820000澳门特别行政区'])){
                $area = new Area();
                $area->region_code = substr($key,0,6);
                $parent_code9 = substr($key,0,6);
                $area->parent_code = 0;
                $area->region_name = substr($key,6);
                $area->region_level = 1;
                $rb = RegionBak::findOne(['region_name'=>$area->region_name]);
                if($rb)
                    $area->firstchar = $rb->firstchar;
                $area->save();
                foreach($value AS $ke=>$va){
                    $area = new Area();
                    $area->region_code = substr($ke,0,6);
                    $parent_code = substr($ke,0,6);
                    $area->parent_code = $parent_code9;
                    $area->region_name = substr($ke,6);
                    $area->region_level = 2;
                    $rb = RegionBak::findOne(['region_name'=>$area->region_name]);
                    if($rb)
                        $area->firstchar = $rb->firstchar;
                    $area->save();
                    if(!$va)
                        continue;
                    foreach($va AS $k=>$v){
                        $area = new Area();
                        $area->region_code = substr($v,0,6);
                        $area->parent_code = $parent_code;
                        $parent_code1 = substr($v,0,6);
                        $area->region_name = substr($v,6);
                        $area->region_level = 3;
                        $area->save();
                        $dist_json = @file_get_contents('http://passer-by.com/data_location/town/'.$area->region_code.'.json');
                        if($dist_json){
                            $final_arr = json_decode($dist_json,true);
                            foreach ($final_arr AS $kk=>$vv){
                                $area = new Area();
                                $area->region_code = $kk;
                                $area->parent_code = $parent_code1;
                                $area->region_name = $vv;
                                $area->region_level = 4;
                                $area->save();
                            }
                        }

                    }
                }
            }else{
                $area = new Area();
                $area->region_code = substr($key,0,6);
                $parent_code = substr($key,0,6);
                $area->parent_code = 0;
                $area->region_name = substr($key,6);
                $area->region_level = 1;
                $rb = RegionBak::findOne(['region_name'=>$area->region_name]);
                if($rb)
                    $area->firstchar = $rb->firstchar;
                $area->save();
                $area = new Area();
                $area->region_code = substr($key,0,3).'100';
                $parent_code9 = substr($key,0,3).'100';
                $area->parent_code = $parent_code;
                $area->region_name = substr($key,6);
                $area->region_level = 2;
                $rb = RegionBak::findOne(['region_name'=>$area->region_name]);
                if($rb)
                    $area->firstchar = $rb->firstchar;
                $area->save();
                if(!$value)
                    continue;
                foreach($value AS $ke=>$va){
                    $area = new Area();
                    $area->region_code = substr($va,0,6);
                    $area->parent_code = $parent_code9;
                    $parent_code1 = substr($va,0,6);
                    $area->region_name = substr($va,6);
                    $area->region_level = 3;
                    $area->save();
                    $dist_json = @file_get_contents('http://passer-by.com/data_location/town/'.$area->region_code.'.json');
                    if($dist_json){
                        $final_arr = json_decode($dist_json,true);
                        foreach ($final_arr AS $kk=>$vv){
                            $area = new Area();
                            $area->region_code = $kk;
                            $area->parent_code = $parent_code1;
                            $area->region_name = $vv;
                            $area->region_level = 4;
                            $area->save();
                        }
                    }

                }
            }
        }
        echo "ok";exit;
    }


}